package com.byyy.ccts.core.utils.cq;

import com.byyy.ccts.core.base.ILogBase;
import com.byyy.ccts.core.utils.CacheUtils;
import com.byyy.ccts.core.utils.JeewebPropertiesUtil;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.core.utils.http.HttpUtils;
import net.sf.json.JSONObject;

/**
 * @projectName: TODO
 * @package: com.byyy.ccts.core.utils.cq
 * @className: CQAuthenticationUtils
 * @description: TODO
 * @author: LYB
 * @Date: 2023-01-11 17:03
 * @updateUser: LYB
 * @updateDate: 2023-01-11 17:03
 * @updateRemark:
 * @version: v1.0
 */
public class CQAuthenticationUtils implements ILogBase {

    /**
     * 第三方appId
     */
    private static String APPID = JeewebPropertiesUtil.getConfig("cq.ll.appId");;

    /**
     * 第三方app 密码
     */
    private static String SECURE = JeewebPropertiesUtil.getConfig("cq.ll.appSecure");;

    /**
     * 数据中心Id
     */
    private static String ACCOUNTID = JeewebPropertiesUtil.getConfig("cq.ll.accountId");;

    /**
     * 数据中心Id
     */
    private static String TENANTID = JeewebPropertiesUtil.getConfig("cq.ll.tenantid");;

    /**
     * 邮件发送人
     */
    private static String USER = JeewebPropertiesUtil.getConfig("cq.ll.user");;

    /**
     * API访问_前缀地址
     */
    public static String URL_HOSTNAME = JeewebPropertiesUtil.getConfig("cq.api.prefix.address");;

    /**
     * 获取应用token_后缀地址
     */
    private static String URL_APP_TOKEN = JeewebPropertiesUtil.getConfig("cq.api.appToken.suffix.address");;

    /**
     * 获取应用accessToken_后缀地址
     */
    private static String URL_ACCESS_TOKEN = JeewebPropertiesUtil.getConfig("cq.api.accessToken.suffix.address");;

    /**
     * 获取APPToken
     *
     * @return
     */
    public static String getAppToken() {
        String appToken = (String) CacheUtils.get(CacheUtils.USER_CACHE, "cq.apptoken");
        if (StringUtils.isNotEmpty(appToken)) {
            return appToken;
        }
        String appTokenUrl = URL_HOSTNAME + URL_APP_TOKEN;
        // 构建参数
        JSONObject tokenObj = new JSONObject();
        tokenObj.put("appId", APPID);
        tokenObj.put("appSecuret", SECURE);
        tokenObj.put("accountId", ACCOUNTID);
        tokenObj.put("tenantid", TENANTID);
        // TODO
        String ret = HttpUtils.httpsPostWithJson(URL_HOSTNAME, appTokenUrl, tokenObj.toString());
        tokenObj = JSONObject.fromObject(ret);

        // 解析结果
        if (null != tokenObj) {
            String status = (String) tokenObj.get("state");
            if ("success".equals(status)) {
                JSONObject dataObj = (JSONObject) tokenObj.get("data");
                appToken = (String) dataObj.get("app_token");
                CacheUtils.put(CacheUtils.USER_CACHE, "cq.ll.apptoken", appToken);
            }
            else {
                logger.error("获取AppToken失败!({})", tokenObj.toString());
            }
        }
        return appToken;
    }

    /**
     * 获取AccessToken
     *
     * @return
     */
    public static String getAccessToken() {
        String accessToken = (String) CacheUtils.get(CacheUtils.USER_CACHE, "cq.ll.accessToken");
        if (StringUtils.isNotEmpty(accessToken)) {
            return accessToken;
        }
        String accessTokenUrl = URL_HOSTNAME + URL_ACCESS_TOKEN;
        accessToken = getAppToken();
        // 构建参数
        JSONObject accessTokenPm = new JSONObject();
        accessTokenPm.put("user", USER);
        accessTokenPm.put("apptoken", accessToken);
        accessTokenPm.put("accountId", ACCOUNTID);
        accessTokenPm.put("usertype", "UserName");
        // TODO
        String ret = HttpUtils.httpsPostWithJson(URL_HOSTNAME, accessTokenUrl, accessTokenPm.toString());
        accessTokenPm = JSONObject.fromObject(ret);

        // 解析结果
        if (null != accessTokenPm) {
            String status = (String) accessTokenPm.get("state");
            if ("success".equals(status)) {
                JSONObject dataObj = (JSONObject) accessTokenPm.get("data");
                accessToken = (String)  dataObj.get("access_token");
                CacheUtils.put(CacheUtils.USER_CACHE, "cq.ll.accessToken", accessToken);
            }
            else {
                logger.error("获取AccessToken失败!({})", accessTokenPm.toString());
            }
        }
        return accessToken;
    }

}
